﻿#ifndef __BPFPI84__
#define __BPFPI84__
#define abs(x) ((x > 0) ? x : (-x))
#define max(a,b) ((a > b) ? a : b)

#define INNER 1;
#define OUTER 0;
//<BPFPI> P93 
//螺旋数。1点的坐标是（0，0）。x 方向向右为正，y 方向向下为正。比如7的坐标为(-1,-1)
//2的坐标为(0,1)，3的坐标为(1,1)。编程实现输入任意一个坐标（x,y），输出对应的数字
class BPFPI84{
 public:
#if INNER
	static int comp(int x, int y){
		int layer = max(abs(x) , abs(y));
		int u = layer*2-1,
			v = u * u;
		if(layer == -x){
			return v + 5*layer - y;
		}else if(layer == y){
			return v + 3*layer - x;
		}else if(layer == -y){
			return v + 7*layer + x;
		}else{
			return v + layer + y;
		}
	}

	static void foo(int n){
		for(int i = -n; i <= n; ++i){
			for(int j = -n;j <= n; ++j)
				printf("%5d",comp(j,i));
			printf("\n");
		}
	}
#endif
#if OUTER
	static void foo(int n){
	int ** arr = new int*[n];
	for(int i = 0;i < n; ++i){
		if((arr[i] =new int[n]) == NULL){
			while(i>=0){
				delete [] arr[i--];
			}
			delete [] arr;
		}
	}
	int cnt = 1;
	//init
	for(int i = 0 ; i < n/2 ; ++i){
		for(int j= i; j <n-i-1; ++j)
			arr[i][j] = cnt++;
		for(int j=i; j<n-i-1; ++j)
			arr[j][n-i-1] = cnt++;
		for(int j=n-i-1;j>i;--j)
			arr[n-i-1][j] = cnt++;
		for(int j=n-i-1;j>i;--j)
			arr[j][i] = cnt++;	
	}

	if(n % 2 != 0)
		arr[n/2][n/2] = cnt;

	//output
	for(int i = 0;i< n;++i){
		for(int j = 0 ;j<n;++j){
			printf("%5d",arr[i][j]);
		}
		printf("\n");
	}

	//clean
	for(int i = 0;i <n; ++i){
		delete [] arr[i];
	}
	delete [] arr;
}
#endif
};
#endif